﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CentaLine.DAL;
using CentaLine.Infrastructure;
using CentaLine.Model;

namespace CentaLine.Repository
{
    /// <summary>
    /// 总监相关业务类
    /// </summary>
    public class DirectorManager
    {
        DirectorDAL directoDAL = null;
        VitualPhoneDAL vitualPhoneDAL = null;
        CounselorManager counselorManager = null;
        public DirectorManager()
        {
            if (directoDAL == null)
                directoDAL = new DirectorDAL();
            if (vitualPhoneDAL == null)
                vitualPhoneDAL = new VitualPhoneDAL();
            if (counselorManager == null)
                counselorManager = new CounselorManager();
        }
        /// <summary>
        ///获取所有总监信息,系统按总监分组处理数据，每次处理一位总监下面的号码分配，避免大批量出错
        /// </summary>
        /// <returns></returns>
        public IList<Director> GetAllDirectorInfoListOAS()
        {
            //return directoDAL.GetAllDirectorInfoListDAL();
            //modify by sunlf  2013-10-14 9:21:16  此处从OAS 库中获取所有总监信息
            return directoDAL.GetAllDirectorListFromOASDAL();
        }

        /// <summary>
        ///获取有虚拟号码的总监信息
        /// </summary>
        /// <returns></returns>
        public IList<Director> GetDirectorInfoListWithVP()
        {
            return directoDAL.GetAllDirectorInfoListDAL();
        }
        /// <summary>
        /// 根据虚拟电话获取总监信息
        /// </summary>
        /// <param name="vitualPhone"></param>
        /// <returns></returns>
        public Director GetDirectorByVp(string vitualPhone)
        {
            Director d= directoDAL.GetDirectorByVpDAL(vitualPhone);
            return d;
        }
        /// <summary>
        ///根据总监获取所有其名下所有可分配的虚拟电话号码
        /// </summary>
        /// <param name="DirectorId">总监工号，为空的话则返回所有</param>
        /// <returns></returns>
        public IList<VitualPhone> GetAllUnUsedPhoneList(string DirectorId="")
        {
           //TODO 返回电话列表是否需要反馈 当前绑定情况
            List<VitualPhone> list = GetAllPhoneList(DirectorId,true);
            list = list.FindAll(v=>v.AssignStatus==AssignStatus.未分配);
            return list;
        }

        /// <summary>
        /// 根据虚拟号码返回虚拟号码信息
        /// </summary>
        /// <param name="vitualPhone">虚拟号码</param>
        /// <returns></returns>
        public List<VitualPhone> GetAllPhoneListByVitualPhone(string vitualPhone)
        {
            List<VitualPhone> list = vitualPhoneDAL.GetAllPhoneListDAL(null, vitualPhone).ToList<VitualPhone>();
            return list;
        }
        /// <summary>
        ///根据总监获取所有其名下所有虚拟电话信息
        /// </summary>
        /// <param name="DirectorId">总监工号</param>
        /// <returns></returns>
        public List<VitualPhone> GetAllPhoneList(string DirectorId = "", bool isNeedLoadCounselor = true,string vitualPhone=null)
        {
            List<VitualPhone> list = vitualPhoneDAL.GetAllPhoneListDAL(DirectorId,vitualPhone).ToList<VitualPhone>();
         
                for (int i = 0; i < list.Count; i++)
                {
                    var v = list[i];
                    var remotelist = vitualPhoneDAL.GetAllPhoneListFromTdialnumberDAL(null, v.VitualPhoneNum).ToList<VitualPhone>();//一个虚拟号码实际只对应一个真实号码
                    if (remotelist.Any())
                    {
                        var item = remotelist[0];
                        v.ActulPhoneNum = item.ActulPhoneNum; //填充真实号码
                        v.AssignStatus = AssignStatus.已分配;
                        if (isNeedLoadCounselor)
                        {
                            var counselor = counselorManager.GetCounrsorByMobile(v.ActulPhoneNum);
                            if (counselor != null)
                            {
                                v.CounselorId = counselor.Id;
                                v.CounselorName = counselor.Name;
                            }
                        }
                    }
                }
            
            return list;
        }
        /// <summary>
        /// 从供应商数据库获取真实生产环境中虚拟电话分配状态
        /// </summary>
        /// <returns></returns>
        public IList<VitualPhone> GetAllPhoneListFromTdialnumber(string actualNo = null, string vitualPhone=null)
        {
            var remotelist = vitualPhoneDAL.GetAllPhoneListFromTdialnumberDAL(actualNo,vitualPhone).ToList<VitualPhone>();//远程数据
            for (int i = 0; i < remotelist.Count;i++ )
            {
                var r = remotelist[i];
                string vp = r.VitualPhoneNum;
                var localItem = GetAllPhoneListByVitualPhone(vp);
                if (localItem.Any())
                {
                    r.PhoneKind = localItem.First().PhoneKind;
                }
                else
                {
                    r.PhoneKind = new PhoneKind(-99, "无法获取该电话的类型");
                    //Logger.Log.Error("远程供应商数据在本地电话池中未找到数据,可能数据异常,对应虚拟号码： " + vp);
                }
            }
            return remotelist;
        }
        /// <summary>
        /// 获取某位总监下所有业务员的虚拟电话申请信息
        /// </summary>
        /// <returns></returns>
        public List<CounselorApplication> GetAllCounselorApplicationList(DateTime applyMonth, string directorId = null, string counselorId = null, string status = null)
        {
            return vitualPhoneDAL.GetAllCounselorApplicationListDAL(applyMonth, directorId,counselorId,status);
        }

    }
}
